<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.blog.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geändert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class Blog {
	var $db;
	
	function __construct() {
		global $db;
		$this->db = $db;
		
		if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
			global $seo_url;
			$this->seo_url = $seo_url;
		}
	}
	
	function blogCategories($post, $action = 'insert') {
		
		$language = get_languages();
	
		if($action == 'insert') {
			$next_id = $this->db->db_query("SELECT MAX(categories_id) AS ID FROM ".TABLE_BLOG_CATEGORIES);
			$next_id = $next_id->fields['ID'];
			if($next_id!=0 ? $next_id+=1 : $next_id=1);
		}
	
		foreach ($language AS $lang) {
			$sql_categorie_array = array('language_id' 	=> $lang['id'],
										'titel' 		=> $_POST['titel'][$lang['id']],
										'position' 	  	=> $_POST['position'][$lang['id']],
										'meta_title'	=> $_POST['meta_title'][$lang['id']],
										'meta_desc'		=> $_POST['meta_desc'][$lang['id']],
										'meta_key'		=> $_POST['meta_key'][$lang['id']]);
	
			if($action == 'insert') {
				$insert_sql_data = array ('categories_id' => $next_id, 'date' => 'NOW()');
				$sql_data_array = array_merge($sql_categorie_array, $insert_sql_data);
				$this->db->db_perform(TABLE_BLOG_CATEGORIES, $sql_data_array);
				$post['categorie_id'] = $next_id;
	
			} elseif($action == 'update') {
				$insert_sql_data = array ('update_date' => 'NOW()');
				$sql_data_array = array_merge($sql_categorie_array, $insert_sql_data);
				$this->db->db_perform(TABLE_BLOG_CATEGORIES, $sql_data_array, 'update', "categories_id = '".$post['categorie_id']."' and language_id = '".$lang['id']."' ");
			}
		}
		if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
			if($action == 'insert')
				$this->seo_url->insertSeoDBTable('blog_cat');
			elseif($action == 'update')
				$this->seo_url->updateSeoDBTable('blog_cat', $post['categorie_id']);
		}
	}
	
	function SetStatus($post) {
		$max_elements = count($post['status']);
		
		if($post['set_cat_status'] == 1 || $post['set_cat_status'] == 2) {
			
			$update_array = array('status' => $post['set_cat_status']);
			for($i = 0; $i < $max_elements; $i++) {
				$this->db->db_perform(TABLE_BLOG_CATEGORIES, $update_array, 'update', "categories_id = '".$post['status'][$i]."'");
				$this->db->db_perform(TABLE_BLOG_ITEMS, $update_array, 'update', "categories_id = '".$post['status'][$i]."'");
			}
		} elseif($_POST['set_cat_status'] == 3) {
			for($i = 0; $i < $max_elements; $i++){
				$this->db->db_query("DELETE FROM ".TABLE_BLOG_CATEGORIES." WHERE categories_id = '".$post['status'][$i]."'");
				$this->db->db_query("DELETE FROM ".TABLE_BLOG_ITEMS." WHERE categories_id = '".$post['status'][$i]."'");
				if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
					$this->seo_url->deleteFromDB('blog_cat', $post['status'][$i]);
					$this->seo_url->deleteFromDB('blog_id', $post['status'][$i]);
				}
			}
		} elseif($post['set_item_status'] == 1 || $post['set_item_status'] == 2) {
			$update_array = array('status' => $post['set_item_status']);
			for($i = 0; $i < $max_elements; $i++)
				$this->db->db_perform(TABLE_BLOG_ITEMS, $update_array, 'update', "item_id = '".$post['status'][$i]."'");
	
		} elseif($_POST['set_item_status'] == 3) {
			for($i = 0; $i < $max_elements; $i++) {
				$this->db->db_query("DELETE FROM ".TABLE_BLOG_ITEMS." WHERE item_id = '".$post['status'][$i]."'");
				$this->db->db_query("DELETE FROM ".TABLE_BLOG_COMMENTS." WHERE blog_id = '".$post['status'][$i]."'");
			}
		}
	}
	
	function blogItem($post, $action = 'insert') {
		$language = get_languages();
	
		if($action == 'insert') {
			$next_id = $this->db->db_query("SELECT MAX(item_id) AS ID FROM ".TABLE_BLOG_ITEMS);
	        $next_id = $next_id->fields['ID'];
	        if($next_id!=0 ? $next_id+=1 : $next_id=1);
		}
	
		foreach($language AS $lang) {
			$sql_item_array = array('language_id'   	=> $lang['id'],
						            'categories_id' 	=> $post['categories_id'],
						            'title' 			=> (!empty($post['title'][$lang['id']]) ? $post['title'][$lang['id']] : $post['name'][$lang['id']]),
						            'name' 				=> $post['name'][$lang['id']],
						            'description' 		=> stripslashes($post['description_'.$lang['id']]),
						            'position' 	    	=> $post['position'],
						            'status' 	    	=> $post['status'],
						            'allow_comment' 	=> $post['allow_comment'],
						            'meta_title'		=> $post['meta_title'][$lang['id']],
						            'meta_description'	=> $post['meta_description'][$lang['id']],
						            'meta_keywords'		=> $post['meta_keywords'][$lang['id']],
						            'lenght'			=> $post['lenght']);
	
			if($action == 'insert') {
		        $insert_sql_data = array ('item_id' => $next_id, 'date' => 'NOW()');
		        $sql_data_array = array_merge($sql_item_array, $insert_sql_data);
				$this->db->db_perform(TABLE_BLOG_ITEMS, $sql_data_array, 'insert');
	
			} elseif ($action == 'update') {
				$insert_sql_data = array ('date_update' => 'NOW()');
				$sql_data_array = array_merge($sql_item_array, $insert_sql_data);
				$this->db->db_perform(TABLE_BLOG_ITEMS, $sql_data_array, 'update', "item_id = '".$post['item']."' and language_id = '".$lang['id']."' ");
			}
		}
		if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
			if($action == 'insert')
				$this->seo_url->insertSeoDBTable('blog_item');
			elseif($action == 'update')
				$this->seo_url->updateSeoDBTable('blog_item', $post['categorie_id']);
		}
	}
}
?>